package util

import (
	"fmt"
	"ordersystem/config"
	"strconv"

	"github.com/gofiber/fiber/v2"
)

func FiberErrorHandler(ctx *fiber.Ctx, err error) error {

	var errorCodeString = err.Error()
	fmt.Println(errorCodeString)
	var errorString string = config.RESPONSE_MESSAGE[errorCodeString]

	errorCode, er := strconv.Atoi(errorCodeString)

	if er != nil {
		errorCode = 500
		errorString = config.RESPONSE_MESSAGE[strconv.Itoa(errorCode)]
	}

	ctx.JSON(map[string]interface{}{
		"code": errorCode,
		"msg":  errorString,
	})

	return nil
}
